<?php
namespace Admin\Controller;
use Lib\Controller\AdminBaseController;
class CouponController extends AdminBaseController{
    private $userModel;
    private $CouponModel;
    private $userCouponModel;
    public function __construct(){
        $this->userModel = D('user');
        $this->CouponModel = D('Coupon');
        $this->userCouponModel = D('userCoupon');
        parent::__construct();
    }
    
    
    public function coupon_list() {
        if(session(C('USER_AUTH_KEY')) != -1){
            $where['server_id'] = session('adminId');
        }
        
        
        $couponList = $this->CouponModel->where($where)->select();
        $this->assign('list',$couponList);
        $this->display();
    }
    
    public function add_coupon(){
        if(IS_POST){
            if(session(C('USER_AUTH_KEY')) != -1){
                $data['server_id'] = session('adminId');
            }
            $data['name'] = I('post.coupon_name');
            $data['type'] = I('post.coupon_type');
            $data['get_type'] = I('post.get_type');
            $data['price'] = I('post.price');
            $data['create_time'] = time();
            $data['start_time'] = strtotime(I('post.start_time'));
            $data['end_time'] = strtotime(I('post.end_time'));
            $addNewCoupon = $this->CouponModel->add($data);
            if($addNewCoupon){
                $info['status'] = 1;
                $info['info'] = '添加成功';
                $info['url'] = U(MODULE_NAME.'/coupon/coupon_list');
                $this->ajaxReturn($info);
            }else{
                $info['status'] = 0;
                $info['info'] = '添加失敗';
                $this->ajaxReturn($info);
            }
        }
        if(IS_GET){
            $this->display();
        }
    }
    
    public function delete_coupon(){
        $where['id'] = I('id');
        $deleteCoupon = $this->CouponModel->where($where)->delete();
        if($deleteCoupon){
            $info['status'] = 1;
            $info['info'] = '刪除成功';
            $info['url'] = U(MODULE_NAME.'/coupon/coupon_list');
            $this->ajaxReturn($info);
        }else{
            $info['status'] = 0;
            $info['info'] = '刪除失敗';
            $this->ajaxReturn($info);
        }
    }
    
    public function send_coupon(){
        $type = I('post.get_type');
        if($type == 1){
            $data['coupon_id'] = I('post.coupon_id');
            $data['member_id'] = -1;
            
            $sendResult = $this->userCouponModel->add($data);
            if($sendResult){
                $info['status'] = 1;
                $info['info'] = '發送成功';
                $this->ajaxReturn($info);
            }else{
                $info['status'] = 0;
                $info['info'] = '系統錯誤';
                $this->ajaxReturn($info);
            }
        }
        else if($type == 0){
            $where['phone'] = I('post.phone');
            $checkUser = $this->userModel->where($where)->find();
            if($checkUser){
                $data['coupon_id'] = I('post.coupon_id');
                $data['member_id'] = $checkUser['id'];
                
                $sendResult = $this->userCouponModel->add($data);
                if($sendResult){
                    $info['status'] = 1;
                    $info['info'] = '發送成功';
                    $this->ajaxReturn($info);
                }else{
                    $info['status'] = 0;
                    $info['info'] = '系統錯誤';
                    $this->ajaxReturn($info);
                }
            }else{
                $info['status'] = 0;
                $info['info'] = '查無該用戶';
                $this->ajaxReturn($info);
            }
        }
        else{
            $info['status'] = 0;
            $info['info'] = '系統錯誤';
            $this->ajaxReturn($info);
        }
    }
}